const app = getApp();
import { mcJudge } from "../../api/score"
import { formatTime } from "../../utils/util"
import Dialog from '../../components/vant/dialog/dialog';
import Toast from '../../components/vant/toast/toast';
Page({
	data: {
		safetyArea: app.globalData.safetyArea + 16,
		formData: {
			scoreProjectId: "",
			name: "",
      avatar: "https://dancebox.cn/images/logo.png",
      mobile: "",
      createTime: "",
		},
		isPost: false
	},
	onLoad(option) {
		if (option.scene) {
			let scene = decodeURIComponent(option.scene)
			this.setData({
				'formData.scoreProjectId': Number(scene.split('t=')[0].split('id=')[1]),
				'formData.createTime': formatTime(new Date(Number(scene.split('t=')[1]))),
			})
		}
		if (option.q) {
			const q = decodeURIComponent(option.q)
			let c = q.split('?')[1].split('&');
			this.setData({
				'formData.scoreProjectId': Number(c[0].split('=')[1]),
				'formData.createTime': formatTime(new Date(Number(c[1].split('=')[1]))),
			})
		}
	},
	changeData(e) { //判断改变数据类型  switch类型 传入type值  取不同值
		let data = JSON.parse(JSON.stringify(this.data.formData));
    	data[e.currentTarget.dataset.value] = e.detail.value;
		this.setData({ formData: data })
	},
	sendActivity() {
		const numReg = /^(13[0-9]|14[56789]|15[0-3,5-9]|16[67]|17[1235678]|18[0-9]|19[135689])\d{8}$/;
		if (this.data.isPost) return false
		let data = JSON.parse(JSON.stringify(this.data.formData))
		if (!this.data.formData.name) {
			this.showError('请输入MC姓名')
			return false;
		} else if (!this.data.formData.mobile) {
			this.showError('请输入手机号')
			return false;
		} else if (!numReg.test(this.data.formData.mobile)) {
			Toast.fail('请输入正确的手机号')
			return false;
		}
		let that = this;
		Dialog.confirm({
			title: '提示',
			message: `你输入的手机号码为：${this.data.formData.mobile} ？`,
		}).then(() => {
			// data.scoreProjectId = 180;
			// data.createTime = formatTime(new Date());
			that.setData({ isPost: true })
			wx.showLoading({ title: '绑定中' })
			mcJudge(data).then(() => {
				that.success('绑定成功', 'none');
				wx.hideLoading()
			}).catch(err =>{
				that.setData({ isPost: false })
				wx.hideLoading()
			})
		}).catch(() => {});
	},
	success(t, type = 'success') { //成功跳转
		wx.hideLoading()
		this.showError(t, type)
		setTimeout(() => {
			wx.redirectTo({ url: `/pages/index/index` })
		}, 1500)
	},
	showError(t, type = 'error') { //展示toast提醒
		wx.showToast({
			title: t,
			icon: type,
			duration: 2000
		})
	},
	qx() {
		wx.redirectTo({ url: `/pages/index/index` })
	}
})